package net.lc7.zkclient;
import net.lc7.model.User;
import net.lc7.util.ZkPropertiesUtil;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @Description:
 * @Author: Jason.zhu
 * @Create: 2019/05/22 17:37
 */

public class ZkClientMain {
    private static ZkClient zkClient;


    public static void main(String[] args) throws Exception{
        String ip_port = ZkPropertiesUtil.getZkServerIp();
        int sessionTimeout = 10000;
        int connetionTimeout = ZkPropertiesUtil.getZktimeout();
        zkClient = new ZkClient(ip_port,sessionTimeout,connetionTimeout,new SerializableSerializer());

        updateNodeData();
    }

    /**
     * 新建节点
     */
    private static void createNode(){
        User user = User.builder().age(18).id(1).name("jason").build();
        String node = zkClient.create("/user",user,CreateMode.PERSISTENT);
        System.out.println(node);
    }

    //获取节点
    private static void getNodeData(){
        Stat stat = new Stat();
        User user1 = zkClient.readData("/user", stat);
        System.out.println("name : " + user1.getName() );
        System.out.println("stat : " + stat);
    }

    /**
     * 创建子节点
     */
    private static void createChildrenNode(){
        String node = zkClient.create("/user/1","user1",CreateMode.PERSISTENT);
        System.out.println(node);
        node = zkClient.create("/user/2","user2",CreateMode.PERSISTENT);
        System.out.println(node);
        node = zkClient.create("/user/3","user3",CreateMode.PERSISTENT);
        System.out.println(node);
    }

    /**
     * 获取子节点
     */
    private static void getChildNode(){
        String node = "/user";
        boolean exist = zkClient.exists(node);
        if(exist){
            List<String> childNodes = zkClient.getChildren(node);
            childNodes.stream().forEach(System.out::println);
        }
    }

    private static void delNode(){
        String node = "/user";
        boolean exist = zkClient.exists(node);
        if(exist){
//           zkClient.delete(node);//删除当前节点，有子节点无法删除
            zkClient.deleteRecursive(node);//删除当前节点，有子节点删除子节点
        }
    }

    /**
     * 更新节点数据
     */
    private static void updateNodeData(){
        User user = User.builder().age(11).name("wangwu").id(12).build();
        String node = "/user";
        zkClient.writeData(node, user);

    }

    /**
     * 监控子节点
     */
    private static void watchChildChange() throws InterruptedException {
        String node = "/user";
        zkClient.subscribeChildChanges(node, (str,strs) -> {
            System.out.println(str);
            System.out.println(strs);
        });
        Thread.sleep(Long.MAX_VALUE);
    }

    /**
     *监控节点
     */
    private static void watchNode(){
        String node ="/user";
        zkClient.subscribeDataChanges(node, new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("node name : " + s + ",updated !!");
                User user = (User)o;
                System.out.println("node new value : " + user);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("node name : " + s + ", deleted !!");
            }
        });

        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
